import { defineStore } from 'pinia'
import {userApi} from "@/api/blog/userApi.ts";

// 第一个参数是应用程序中 store 的唯一 id
export const userStore = defineStore('user', {
  // 推荐使用 完整类型推断的箭头函数
  state: () => {
    return {
      nickname: '',
      avatar: ''
    } as User
  },
  getters: {
    getUserNikeName: (state) => {
      return state.nickname
    },
    getUserAvatar: (state) => {
      return state.avatar
    },
    isLogin: () => {
      return localStorage.getItem('token') != null
    }
  },
  actions: {
    async initUser () {
      await userApi.getUserInfo().then((res) => {
        this.$state = res.data
      })
    }
  },
  // 表示这个store里的数据都将持久化存储
  persist: true
})
